function clickContent(index){
	contentNow = index;
	$(".active").removeClass("active");
	$("#c_"+index).addClass("active");
	$(".content").slideUp();
	$("#content_"+index).slideDown();
	switch(index){
		case 1: readyContent_1(1); break;
		case 2: readyContent_2(1); break;
		//case 3: readyContent_3(1); break;
	}
}

var activitys_add = "";
var merkets_add = "";
var activityArray = new Array();
var marketArray = new Array();

function saveContent(type){
	switch(type){
	case 1: 
		activitys_add = "";
		/*$("input[name='activityCheckBox']:checkbox:checked").each(function(){ 
			if($(this).attr("checked")){
				activitys_add += $(this).val()+",";
			}
		});*/
		for(var i=0; i<activityArray.length; i++){
			activitys_add += activityArray[i]+",";
		}
		clickContent(2);
		break;
	case 2: 
		merkets_add = "";
		for(var i=0; i<marketArray.length; i++){
			merkets_add += marketArray[i]+",";
		}
		clickContent(3);
		break;
	case 3: 
		$("#activityStr").val(activitys_add);
		$("#marketStr").val(merkets_add);
		save();
		break;
	}
}

function addRemoveActivity(index){
	if($("#activityBox_"+index).attr("checked")=="checked"){
		activityArray.push($("#activityBox_"+index).val());
	}else{
		for(var i=0; i<activityArray.length; i++){
			if($("#activityBox_"+index).val() == activityArray[i]){
				activityArray.splice(i,1);
			}
		}
	}
}

function addRemoveMarket(index){
	if($("#marketBox_"+index).attr("checked")=="checked"){
		marketArray.push($("#marketBox_"+index).val());
	}else{
		for(var i=0; i<marketArray.length; i++){
			if($("#marketBox_"+index).val() == marketArray[i]){
				marketArray.splice(i,1);
			}
		}
	}
}

function readyContent_1(pageNow){
	var tmp2=root+"/admin/activity-admin!findList";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{"page.curPageNum":pageNow, "isDesc": isDesc, "brand.id":"", "page.numberPerPage": 10},
		dataType:"json",
		success: function(json){
			var htmlStr = '';
			for(var i=0; i<json.pageData.length; i++){
				htmlStr += '<li style="width: 100%;"><label style="cursor: pointer;">',
				htmlStr += '<input id="activityBox_'+i+'" type="checkBox" name="activityCheckBox" value="'+json.pageData[i].id+'"';
				for(var j=0; j<activityArray.length; j++){
					if(json.pageData[i].id == activityArray[j]){
						htmlStr += ' checked="checked"';
					}
				}
				htmlStr += ' onclick="addRemoveActivity('+i+')" />';
				htmlStr += '&nbsp;&nbsp;&nbsp;'+json.pageData[i].subject+'&nbsp;&nbsp;&nbsp;'+json.pageData[i].content+'</label></li>';
			}
			htmlStr += '<li style="text-align: left; cursor: pointer; width: 80%; border-bottom:none;">';
			htmlStr += '<span onclick="readyContent_1('+1+')">首页</span>';
			if(json.curPageNum>1){
				htmlStr += '<span onclick="readyContent_1('+(json.curPageNum-1)+')">上一页</span>';
			}else{
				htmlStr += '<span>上一页</span>';
			}
			if((json.curPageNum+1)<=json.totalPageNum){
				htmlStr += '<span onclick="readyContent_1('+(json.curPageNum+1)+')">下一页</span>';
			}else{
				htmlStr += '<span>下一页</span>';
			}
			
			htmlStr += '<span onclick="readyContent_1('+json.totalPageNum+')">尾页</span>';
			htmlStr += '<span>('+json.curPageNum+'/'+json.totalPageNum+')</span>';
			htmlStr += '</li>';
			htmlStr += '<li style="text-align: right; cursor: pointer; width: 20%;">';
			htmlStr += '<span onclick="saveContent(1)">下一步</span></li>';
			$("#activityUl").html(htmlStr);
		}
	});
}

function readyContent_2(pageNow){
	var tmp2=root+"/admin/super-market!findSuperMarketByCompanyId";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{"page.curPageNum":pageNow, "isDesc": isDesc, "company.id":"", "page.numberPerPage": 20},
		dataType:"json",
		success: function(json){
			var htmlStr = '';
			for(var i=0; i<json.pageData.length; i++){
				htmlStr += '<li><label style="cursor: pointer;"><input id="marketBox_'+i+'" type="checkBox" name="marketCheckBox"';
				for(var j=0; j<marketArray.length; j++){
					if(json.pageData[i].id == marketArray[j]){
						htmlStr += ' checked="checked"';
					}
				}
				htmlStr += ' value="'+json.pageData[i].id+'" onclick="addRemoveMarket('+i+')" />';
				htmlStr += '&nbsp;&nbsp;&nbsp;'+json.pageData[i].company.name+' - '+json.pageData[i].name+'</label></li>';
			}
			htmlStr += '<li style="text-align: left; cursor: pointer; width: 75%; border-bottom:none;">';
			htmlStr += '<span onclick="readyContent_2('+1+')">首页</span>';
			if(json.curPageNum>1){
				htmlStr += '<span onclick="readyContent_2('+(json.curPageNum-1)+')">上一页</span>';
			}else{
				htmlStr += '<span>上一页</span>';
			}
			if((json.curPageNum+1)<=json.totalPageNum){
				htmlStr += '<span onclick="readyContent_2('+(json.curPageNum+1)+')">下一页</span>';
			}else{
				htmlStr += '<span>下一页</span>';
			}
			
			htmlStr += '<span onclick="readyContent_2('+json.totalPageNum+')">尾页</span>';
			htmlStr += '<span>('+json.curPageNum+'/'+json.totalPageNum+')</span>';
			htmlStr += '</li>';
			htmlStr += '<li style="text-align: right; cursor: pointer; width: 25%;">';
			htmlStr += '<span onclick="clickContent(1)">上一步</span><span onclick="saveContent(2)">下一步</span></li>';
			$("#marketUl").html(htmlStr);
		}
	});
}

function readyContent_3(pageNow){
	var s = $("#marketUl").html();
	if(s.Trim()!="" &&s.Trim().length>0){
		return;
	}
}

function save(){
	if(activitys_add==""){
		alert("请至少选择一个活动");
		return;
	}
	if(merkets_add==""){
		alert("请至少选择一个门店");
		return;
	}
	if(!verificationNumber("haibaoInput")){
		alert("请输入正确的海报数量");
		return;
	}
	if(!verificationNumber("duitouInput")){
		alert("请输入正确的堆头数量");
		return;
	}
	if(!verificationNumber("toutaoInput")){
		alert("请输入正确的头套数量");
		return;
	}
	if(!verificationNumber("duanjiaInput")){
		alert("请输入正确的端架数量");
		return;
	}
	if(!verificationNumber("chenlieInput")){
		alert("请输入正确的陈列数量");
		return;
	}
	$("#addForm").submit();
}

//是否为数字且不为空
function verificationNumber(divId){
	var s = $("#"+divId).val();
	if(s=="" || s.length<1) return false;
	var patrn=/^[0-9]*$/; 
	if (!patrn.exec(s)) return false ;
	return true;
}